package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.query.ProductQueryObject;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.PageResult;
import cn.wolfcode.wms.util.StringUtil;
import cn.wolfcode.wms.util.UploadUtil;
import com.sun.org.apache.xpath.internal.operations.Mult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.Optional;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ServletContext ctx;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") ProductQueryObject qo, Model model){
        PageResult result = productService.query(qo);
        model.addAttribute("result", result);
        model.addAttribute("brands", brandService.list());
        return "product/list";
    }

    @RequestMapping("listView")
    public String listView(@ModelAttribute("qo") ProductQueryObject qo, Model model){
        qo.setPageSize(20); //写死,方便查看数据
        PageResult result = productService.query(qo);
        model.addAttribute("result", result);
        model.addAttribute("brands", brandService.list());
        return "product/listView";
    }

    @RequestMapping("input")
    public String input(Long id, Model model){
        model.addAttribute("entity", productService.get(id));
        model.addAttribute("brands", brandService.list());
        return "product/input";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Product entity, MultipartFile pic){
        //处理图片
        if (pic != null && pic.getContentType().startsWith("image") && pic.getSize() > 0){

            //是否需要删除旧的图片
            if (StringUtils.hasLength(entity.getImagePath())) {
                UploadUtil.deleteFile(ctx, entity.getImagePath());
            }

            //符合上传要求的图片
            String path = UploadUtil.upload(pic, ctx.getRealPath("/upload"));
            entity.setImagePath(path);
        }
        productService.saveOrUpdate(entity);
        return new JSONResult();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        String imagePath = productService.get(id).getImagePath();
        if (Optional.ofNullable(id).isPresent()){ //判断是否有id
            productService.delete(id);
        }
        //删除数据完成后再删除图片
        if (StringUtils.hasLength(imagePath)) {
            UploadUtil.deleteFile(ctx, imagePath);
        }
        return new JSONResult();
    }
}
